home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / emacs / detexinfo.el < prev    next >
Lisp/Scheme  |  1994-01-28  |  10KB  |  251 lines

  1. ;;; Here is a handy keybinding: 
  2.  
  3. (global-set-key "\C-x\\" 'detexinfo)
  4.  
  5. ;;;;;;;;;;;;;;;; detexinfo.el ;;;;;;;;;;;;;;;;
  6. ;;;
  7. ;;; Remove Texinfo commands from a Texinfo source file.
  8. ;;;
  9. ;;; Copyright (C) 1991, 1992 Free Software Foundation
  10. ;;; Robert J. Chassell
  11. ;;; bugs to bug-texinfo@prep.ai.mit.edu
  12. ;;;
  13. ;;; ==> test version <==
  14. ;;; Fails if Texinfo source file contains formatting errors.
  15. ;;; 
  16. ;;; Version 0.05 -  3 Jun 1992
  17. ;;; Add to list of removed commands.  Improve messages.
  18. ;;;
  19. ;;; Version 0.04 - 27 Jan 1992
  20. ;;; Rewrite to insert detexinfo'd text into a temporary buffer.
  21. ;;; 
  22. ;;; Version 0.03 - 27 Dec 1991
  23. ;;; Improved messages.
  24. ;;;
  25. ;;; Version 0.02 - 13 Nov 1991
  26. ;;; detexinfo-remove-inline-cmd, detexinfo-syntax-table: Handle 
  27. ;;;          nested commands.  
  28. ;;; detexinfo: Handle nested @'s, eg @samp{@}} and @samp{@@};
  29. ;;;          replace @TeX{} with TeX.
  30. ;;; 
  31. ;;; Version 0.01 - 13 Nov 1991
  32. ;;;
  33. ;;; Based on detex.el, by Bengt Martensson, 4 Oct 1987
  34. ;;;
  35. ;;;;;;;;;;;;;;;;
  36.  
  37. (defvar detexinfo-buffer-name "*detexinfo*"
  38.   "*Name of the temporary buffer used by \\[detexinfo].")
  39.  
  40. (defvar detexinfo-syntax-table nil)
  41.  
  42. (if detexinfo-syntax-table
  43.     nil
  44.   (setq detexinfo-syntax-table (make-syntax-table))
  45.   (modify-syntax-entry ?\[ "." detexinfo-syntax-table)
  46.   (modify-syntax-entry ?\] "." detexinfo-syntax-table)
  47.   (modify-syntax-entry ?\" "." detexinfo-syntax-table)
  48.   (modify-syntax-entry ?\\ "." detexinfo-syntax-table)
  49.   (modify-syntax-entry ?\( "." detexinfo-syntax-table)
  50.   (modify-syntax-entry ?\) "." detexinfo-syntax-table)
  51.   (modify-syntax-entry ?{ "(}" detexinfo-syntax-table)
  52.   (modify-syntax-entry ?} "){" detexinfo-syntax-table))
  53.  
  54. (defun detexinfo ()
  55.   "Remove Texinfo commands from current buffer, copying result to new buffer.
  56. BUG: Fails if Texinfo source file contains formatting errors."
  57.   (interactive)
  58.   (let ((input-buffer (current-buffer)))
  59.     ;; Find a buffer to use.
  60.     (switch-to-buffer (get-buffer-create detexinfo-buffer-name))
  61.     (setq major-mode 'detexinfo-mode)
  62.     (set-syntax-table detexinfo-syntax-table)
  63.     (erase-buffer)
  64.     (insert-buffer-substring input-buffer)
  65.     
  66.     ;; Replace @{ and @} with %#* and *#% temporarily, so @samp{@{} works.
  67.     ;; What is a better way of doing this??
  68.     (goto-char (point-min))
  69.     (while (search-forward "@{" nil t)  ; e.g., @samp{@{}
  70.       (replace-match "%#*"))
  71.     (goto-char (point-min))
  72.     (while (search-forward "@}" nil t)
  73.       (forward-char -3)                 ; e.g.,  @samp{@@}
  74.       (if (looking-at "@")              ; Two @@ in a row
  75.           (progn
  76.             (delete-char 2)
  77.             (insert "%&%#"))
  78.         (forward-char 1)
  79.         (delete-char 2)
  80.         (insert "*#%")))
  81.     
  82.     (goto-char (point-min))
  83.     ;; Remove @refill, the only inline command without braces.
  84.     (while (search-forward "@refill" nil t)
  85.       (replace-match ""))
  86.     ;; Replace @TeX{} with TeX
  87.     (goto-char (point-min))
  88.     (while (search-forward "@TeX{}" nil t) (replace-match "TeX" t t))
  89.     
  90.     (detexinfo-remove-line-cmds-without-arg)
  91.     (detexinfo-remove-inline-cmds-without-arg)
  92.     (detexinfo-remove-inline-cmds-keep-arg)
  93.     (detexinfo-remove-line-cmds-deletable-arg)
  94.     (detexinfo-remove-line-cmds-maybe-delete-arg)
  95.     (detexinfo-remove-line-cmds-keep-arg)
  96.     
  97.     ;; Now replace %#*, *#%, and %&%#  with {,  }, and @@.
  98.     (goto-char (point-min))
  99.     (while (search-forward "%#*" nil t)
  100.       (replace-match "{"))
  101.     (goto-char (point-min))
  102.     (while (search-forward "*#%" nil t)
  103.       (replace-match "}"))
  104.     (goto-char (point-min))
  105.     (while (search-forward "%&%#" nil t)
  106.       (replace-match "@@"))
  107.     
  108.     ;; Scan for remaining two character @-commands
  109.     (goto-char (point-min))
  110.     (while (search-forward "@" nil t)
  111.       (cond ((looking-at "[*:]")
  112.              (delete-region (1- (point)) (1+ (point))))
  113.             ((looking-at "[{}@.'`]")
  114.              (delete-region (1- (point)) (point)))))
  115.     
  116.     (goto-char (point-min))
  117.     (message "Done...removed Texinfo commands from buffer. You may save it.")))
  118.  
  119. (defun detexinfo-remove-whole-line (cmd)
  120.   "Delete Texinfo line command CMD at beginning of line and rest of line."
  121.   (goto-char (point-min))
  122.   (while
  123.       (re-search-forward
  124.        (concat "^@" cmd "[ \n]+") (point-max) t)
  125.     (goto-char (match-beginning 0))
  126.     (delete-region
  127.      (point) (save-excursion (end-of-line) (1+ (point))))))
  128.  
  129. (defun detexinfo-remove-inline-cmd (cmd)
  130.   "Delete Texinfo inline command CMD, eg. @point, @code."
  131.   (goto-char (point-min))
  132.   (while
  133.       (re-search-forward (concat "@" cmd "{") (point-max) t)
  134.     (save-excursion
  135.       (forward-char -1)
  136.       (forward-sexp 1)
  137.       (delete-char -1))                 ; delete right brace
  138.     (delete-region (point) (match-beginning 0)))) 
  139.  
  140. ;;;;;;;;;;;;;;;; 
  141.  
  142. ;;; 1. @setfilename and other line commands with args to delete
  143.  
  144. (defvar detexinfo-line-cmds-deletable-arg
  145.   '("enumerate" "ftable" "vtable" "itemize" "table"
  146.     "setfilename" "settitle" "setchapternewpage" 
  147.     "footnotestyle" "paragraphindent"
  148.     "include" "need" "sp" 
  149.     "clear" "ifclear" "ifset"  "set"
  150.     "defcodeindex" "defindex" "syncodeindex" "synindex")
  151.   "List of Texinfo commands whose arguments should be deleted.")
  152.  
  153. (defun detexinfo-remove-line-cmds-deletable-arg ()
  154.   "Delete Texinfo line commands together with their args, eg @setfilename."
  155.   (message "Removing commands such as @enumerate...with their arguments...")
  156.   (mapcar 'detexinfo-remove-whole-line
  157.           detexinfo-line-cmds-deletable-arg))
  158.  
  159. ;;; 2. @cindex and other cmds with args that may be deleted
  160. ;;;    This list is here just to make it easier to revise the
  161. ;;;    categories.  In particular, you might want to keep the index entries.
  162.  
  163. (defvar detexinfo-line-cmds-maybe-delete-arg
  164.    '("cindex" "findex" "kindex" "pindex" "tindex" "vindex" "node"
  165.      "c" "comment" "end" "headings"  "printindex" "vskip" 
  166.      "evenfooting"  "evenheading" "everyfooting" "everyheading" 
  167.      "oddfooting" "oddheading")
  168.   "List of Texinfo commands whose arguments may possibly be deleted.")
  169.  
  170. (defun detexinfo-remove-line-cmds-maybe-delete-arg ()
  171.   "Delete Texinfo line commands together with their arguments, eg, @cindex."
  172.   (message "Removing commands such as @cindex...with their arguments...")
  173.   (mapcar 'detexinfo-remove-whole-line
  174.           detexinfo-line-cmds-maybe-delete-arg))
  175.  
  176. ;;; 3. @chapter and other line cmds with args to keep.
  177.  
  178. (defvar detexinfo-line-cmds-keep-arg
  179.    '("top" "chapter" "section" "subsection" "subsubsection" 
  180.      "unnumbered" "unnumberedsec" "unnumberedsubsec" "unnumberedsubsubsec" 
  181.      "majorheading" "chapheading" "heading" "subheading" "subsubheading" 
  182.       "appendix" "appendixsec" "appendixsubsec" "appendixsubsubsec" 
  183.      "item" "itemx"
  184.      "title" "subtitle" "center" "author" "exdent"  
  185.      "defcv" "deffn" "defivar" "defmac" "defmethod" "defop" "defopt" 
  186.      "defspec" "deftp" "deftypefn" "deftypefun" "deftypvr"
  187.      "deftypevar" "defun" "defvar" "defvr")
  188.   "List of Texinfo line commands whose arguments should be kept.")
  189.  
  190. (defun detexinfo-remove-line-cmds-keep-arg ()
  191.   "Delete Texinfo line commands but keep their arguments, eg @chapter."
  192.   (message "Removing commands such as @chapter...but not their arguments...")
  193.   (mapcar 'detexinfo-remove-line-cmd-keep-arg
  194.           detexinfo-line-cmds-keep-arg))
  195.  
  196. (defun detexinfo-remove-line-cmd-keep-arg (cmd)
  197.   "Delete Texinfo line command CMD but keep its argument, eg @chapter."
  198.   (goto-char (point-min))
  199.   (while
  200.       (re-search-forward
  201.        (concat "^@" cmd "[ \n]+") (point-max) t)
  202.     (delete-region (match-beginning 0) (match-end 0))))
  203.  
  204. ;;; 4. @bye and other line commands without args.
  205.  
  206. (defvar detexinfo-line-cmds-without-arg
  207.   '("bye" "contents" "display" "example" "finalout" 
  208.     "flushleft" "flushright" "format" "group" "ifinfo" "iftex" 
  209.     "ignore"  "lisp" "menu" "noindent" "page" "quotation"  
  210.     "shortcontents" "smallbook" "smallexample" "smalllisp" 
  211.     "summarycontents" "tex" "thischapter" "thischaptername" 
  212.     "thisfile" "thispage" "thissection" "thistitle" "titlepage")
  213.   "List of Texinfo commands without arguments that should be deleted.")
  214.  
  215. (defun detexinfo-remove-line-cmds-without-arg ()
  216.   "Delete line Texinfo commands that lack args, eg. @example."
  217.   (message "Removing commands such as @example...that lack arguments...")
  218.   (mapcar 'detexinfo-remove-whole-line
  219.           detexinfo-line-cmds-without-arg))
  220.  
  221. ;;; 5. @equiv and other inline cmds without args.
  222.  
  223. (defvar detexinfo-inline-cmds-without-arg
  224.   '("equiv" "error" "expansion" "point" "print" "result"
  225.     "asis"  "br" "bullet" "dots" "minus" "today")
  226.   "List of Texinfo inline commands without arguments that should be deleted.")
  227.  
  228. (defun detexinfo-remove-inline-cmds-without-arg ()
  229.   "Delete Texinfo inline commands in that lack arguments."
  230.   (message "Removing within line commands such as @result...")
  231.   (mapcar 'detexinfo-remove-inline-cmd
  232.           detexinfo-inline-cmds-without-arg))
  233.  
  234. ;;; 6. @code and other inline cmds with args to keep
  235.  
  236. (defvar detexinfo-inline-cmds-keep-arg
  237.   '("b" "cartouche" "cite" "code" "copyright" "ctrl" "dfn" "dmn"
  238.     "emph" "file" "footnote" "i" "inforef"
  239.     "kbd" "key" "pxref" "r" "ref" "samp" "sc" "titlefont" 
  240.     "strong" "t" "var" "w" "xref")
  241.   "List of Texinfo inline commands with arguments that should be kept.")
  242.  
  243. (defun detexinfo-remove-inline-cmds-keep-arg ()
  244.   "Delete Texinfo inline commands but keep its arg, eg. @code."
  245.   (message
  246.    "Removing within line commands such as @code...but not their arguments...")
  247.   (mapcar 'detexinfo-remove-inline-cmd
  248.           detexinfo-inline-cmds-keep-arg))
  249.  
  250. ;;;;;;;;;;;;;;;; end detexinfo.el ;;;;;;;;;;;;;;;;
  251.